package com.cb.util;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.StatFs;

// 通用的方法

/* 
 * 1. 禁止UI连续点击， sample
public void onClick(View v) {
if (Utils.isFastDoubleClick()) {
    return;
}
}*/

public class Utils {
	private static long lastClickTime;

	public static boolean isFastDoubleClick() {
		long time = System.currentTimeMillis();
		long timeD = time - lastClickTime;
		if (0 < timeD && timeD < 500) {
			return true;
		}
		lastClickTime = time;
		return false;
	}

	public static void delete(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			file.delete();
			return;
		}
		String[] nlist = file.list();
		File temp = null;
		for (int i = 0; i < nlist.length; ++i) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + nlist[i]);
			} else {
				temp = new File(path + File.separator + nlist[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			} else if (temp.isDirectory()) {
				delete(temp.getAbsolutePath());
			}
		}
		// delete folder
		file.delete();
	}
//	public static void delete(File file) {
//		if (file.isFile()) {
//			file.delete();
//			return;
//		}
//
//		if (file.isDirectory()) {
//			File[] childFiles = file.listFiles();
//			if (childFiles == null || childFiles.length == 0) {
//				file.delete();
//				return;
//			}
//
//			for (int i = 0; i < childFiles.length; i++) {
//				delete(childFiles[i]);
//			}
//			file.delete();
//		}
//	}
	public static boolean isNetConnected(Context context) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					if (info.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}
	public static boolean isWifiConnected(Context context) {
		return getNetworkState(context, ConnectivityManager.TYPE_WIFI) == State.CONNECTED;
	}
	private static State getNetworkState(Context context, int networkType) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getNetworkInfo(networkType);

		return info == null ? null : info.getState();
	}
	
	static public String toSDSpace(long bytes){
		if(bytes < 100){
			return Long.toString(bytes)+"b";
		}
		double space = bytes/1024;
		if(space < 100){
			int s = (int)space;
			return Integer.toString(s)+"Kb";
		}
		space = space/1024;
		if(space < 100){
			int s = (int)space;
			return Integer.toString(s)+"MB";
		}else{
			space = space/1024;
			DecimalFormat df = new DecimalFormat();
			df.setMaximumFractionDigits(2);
			df.setMinimumFractionDigits(2);
			return df.format(space)+"GB";
		}
	}
	static public long getFreeSpace(String path) {
		StatFs sf = new StatFs(path);
		long blockSize = sf.getBlockSize();
		// long blockCount = sf.getBlockCount();
		long availCount = sf.getAvailableBlocks();
		long freeSpace = availCount * blockSize;
		return freeSpace;
	}
	static public String getSDSpaceInfo(String path){
		String name = new File(path).getName();
		if(name == null || name.length() == 0){
			return "";
		}
		long free = getFreeSpace(path);
		long total = getTotalSpace(path);			
		String info = name + "\n可用空间"+toSDSpace(free)+"\n总容量:"+toSDSpace(total);
		return info;
	}
	
	//scan all sd that maybe sd card under /storage/ dir
	static public ArrayList<String> scanAllMaybeSD(boolean isForpic) {
		ArrayList<String> allSD = new ArrayList<String>();
		File fileold = new File("/storage/");
		try {
			File[] files = fileold.listFiles();
			if (files.length > 0) {
				for (int j = 0; j < files.length; j++) {
					String fName = files[j].getName();
					if (fName.contains("Sd") || fName.contains("sd")) {
						if (files[j].isDirectory()) {
							if (isForpic || getTotalSpace(files[j].getPath()) > 1024 * 1024 * 1000) { // 1G
								allSD.add(files[j].getPath());
							}
						}
					}
				}
			}
		} catch (Exception e) {

		}
		
		if (allSD.size() < 1) {
			fileold = new File("/mnt/");
			try {
				File[] files = fileold.listFiles();
				if (files.length > 0) {
					for (int j = 0; j < files.length; j++) {
						String fName = files[j].getName();
						if (fName.contains("Sd") || fName.contains("sd")) {
							if (files[j].isDirectory()) {
								if (isForpic || getTotalSpace(files[j].getPath()) > 1024 * 1024 * 500) { // 500mb
									allSD.add(files[j].getPath());
								}
							}
						}
					}
				}
			} catch (Exception e) {

			}
		}
		
		if (allSD.size() < 1) {
			fileold = new File("/");
			try {
				File[] files = fileold.listFiles();
				if (files.length > 0) {
					for (int j = 0; j < files.length; j++) {
						String fName = files[j].getName();
						if (fName.contains("Sd") || fName.contains("sd")) {
							if (files[j].isDirectory()) {
								if (isForpic || getTotalSpace(files[j].getPath()) > 1024 * 1024 * 1000) { // 1G
									allSD.add(files[j].getPath());
								}
							}
						}
					}
				}
			} catch (Exception e) {

			}
		}
		
		return allSD;
	}
	
	static public long getTotalSpace(String path) {
		StatFs sf = new StatFs(path);
		long blockSize = sf.getBlockSize();
		long blockCount = sf.getBlockCount();
		//long availCount = sf.getAvailableBlocks();
		long totalSpace = blockCount * blockSize;
		return totalSpace;
	}
	
	static public String getDeviceInfo(){
		String model = android.os.Build.MODEL; 
		int sdkVer = android.os.Build.VERSION.SDK_INT; 
		String relVer = android.os.Build.VERSION.RELEASE;
		String brand = android.os.Build.BRAND;
		String manu = android.os.Build.MANUFACTURER;
		//String user = android.os.Build.USER;
		JSONObject json = new JSONObject();
		try {
			json.put("model", model);
			json.put("sdk", sdkVer);
			json.put("ver", relVer);
			json.put("brand", brand);
			json.put("manu", manu);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toString();
	}
}
 

